class Register{
    constructor(){
        this.logoinBtn = document.querySelector("#logoinBtn");
        this.tips = document.querySelector("#tips");
        this.emailElement = document.querySelector("#email");        
        this.passwordElement = document.querySelector("#password");        
        this.userElement = document.querySelector("#user"); 
    }
    addEvent(){
        var that = this;
        this.logoinBtn.onclick=function(){
            that.email = that.emailElement.value;        
            that.password = that.passwordElement.value;        
            that.user = that.userElement.value; 
            that.judgeUser();
        }
    }
    judgeUser(){
        // 判读用户名只能是手机号
        var userReg = /^\d{11}$/;
        // 判断密码只能是数字小写字母大写字母和/*-+.
        var passwordrReg = /^[0-9a-z-A-Z\/\*\-\+\.]{6,13}$/;
        // 判断邮箱
        var emailReg = /^[0-9a-zA-Z]{4,13}@[0-9a-zA-Z]{2,9}\.[a-zA-Z]{2,4}$/;
        
        this.tips.style.display = "none";
        
        if(!userReg.test(this.user)){
            this.tips.style.display = "block";
            this.tips.innerText = "您的手机号格式错误，请重新输入";
            this.userElement.value = '';
        }
        if(!passwordrReg.test(this.password)){
            this.tips.style.display = "block";
            this.tips.innerText = "您的密码格式错误，请重新输入";
            this.passwordElement.value = '';
        }
        if(!emailReg.test(this.email)){
            this.tips.style.display = "block";
            this.tips.innerText = "您的邮箱输入有误，请重新输入";
            this.emailElement.value = '';
        }
        if(userReg.test(this.user)&&passwordrReg.test(this.password)&&emailReg.test(this.email)){
            this.ajax();
        }
    }

    ajax(){
        var that=this;
        var name =parseInt(Math.random()*10);
        for(var i = 0 ; i < 6 ; i++){
            name += String(parseInt(Math.random()*10));
        }
        var str = {
            url:"http://localhost/XMYP/src/php/register.php",
            type:"get",
            data:{
                user:that.user,
                password:that.password,
                email:that.email,
                name:name,
            }
        };
        var p1 = pAjax(str);
        p1.then(function(res){
            var r = JSON.parse(res);
            if(r.code==2){
                that.tips.style.display="block";
                that.tips.innerText="注册成功，3秒后进入登录界面";
                that.dump();
            }
        });
    }
    dump(){
        setInterval(function(){
            window.location.href = "http://localhost/XMYP/src/pages/login.html";
        },3000)
    }
}

var register = new Register();
register.addEvent();